﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using TeamService.Access;

namespace TeamService.ViewModal
{
    public class AuthorRight : INotifyPropertyChanged
    {
        public AuthorRight() { _authorRight = 0; }
        public event PropertyChangedEventHandler PropertyChanged;
        int _authorRight;
        public int Right {
            get {
                return _authorRight;
            }
            set {
                _authorRight = value;
                Update(nameof(Right));
            }
        }

        private void Update(String v)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
        }
    }
    public class TeamServieceViewModal : INotifyPropertyChanged
    {

        #region PropertChangedSuppoerted
        public event PropertyChangedEventHandler PropertyChanged;
        void Update(string Property)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Property)));
        }

        #endregion

        TeamServeiceAccessor _tsa;

        public TeamServieceViewModal()
        {
            _tsa = new TeamServeiceAccessor();
            _loaded = false;
        }

        bool _loaded;
        public bool Load()
        {
            if (!_loaded)
            {
                var mem = _tsa.SelectMember();
                _loaded = true;

                if (mem != null)
                    foreach (var i in mem)
                        Members.Add(i);

                var p = _tsa.SelectProject();

                if (p != null)
                {
                    foreach (var i in p)
                    {
                        var team = _tsa.FindTeamInProject(i);
                        i.Teams = new ObservableCollection<Team>();
                        foreach (var n in team)
                        {
                            i.Teams.Add(n);
                        }
                        Projects.Add(i);
                    }
                }

                var t = _tsa.SelectTeam();

                if (t != null)
                {
                    foreach (var i in t)
                    {
                        var member = _tsa.FindMemberInTeam(i);
                        i.Member = new ObservableCollection<Member>();
                        foreach (var n in member)
                        {
                            i.Member.Add(n);
                        }
                        Team.Add(i);
                    }
                }
                if (t == null && p == null && mem == null)
                    return false;
                return true;
            }
            else
            {
                return false;
            }
        }

        public ObservableCollection<Project> Projects { get; set; }
            = new ObservableCollection<Project>();

        public ObservableCollection<Member> Members { get; set; }
            = new ObservableCollection<Member>();

        public ObservableCollection<Team> Team { get; set; }
            = new ObservableCollection<Team>();

        bool _isLogin;
        public bool IsLogin {
            get {
                return _isLogin;
            }
            set {
                _isLogin = value;
                Update(nameof(IsLogin));
            }
        }

        public AuthorRight AuthorRight { get; set; } = new AuthorRight();
        public void RefreshAuthorRight(Member member)
        {
            AuthorRight.Right = _tsa.SelectRight(member);
        }


        string _loginName;
        public string LoginName {
            get {
                return _loginName;
            }
            set {
                _loginName = value;
                Update(nameof(LoginName));
            }
        }

        public Action<int> Navigate { get; set; }

        public object Selector { get; set; }
        public object Focus { get; set; }
        public object SelectItem { get; set; }
        public object MemberList { get; set; }

        public Queue<WorkItem> WorkQueue { get; set; } = new Queue<WorkItem>();
    }
}
